#include "wangge.h"
#include <QPainter>
#include <QTime>
#include <QTimer>
#include <QString>



wangge::wangge(QWidget *parent)
    : QMainWindow(parent)
{
    resize(1000,1000);
    time = new QLabel(this);
    time->resize(100,100);
    time->move(0,600);
    time->setText("1234");
    int i=0;
    for(i=0;i<21;i++)
    {
        x[i]=10*i;
        y[i]=200;
    }
    for(i=0;i<21;i++)
    {
        while(1)
        {
          y1[i]=qrand()%500;
          if(y1[i]>299) break;
        }
    }
    this->startTimer(1000);
}

wangge::~wangge()
{


}

void wangge::paintEvent(QPaintEvent *)
{

    int i=0;
    int a=20,b=20,c=20,d=300;
    QPainter painter(this);
    QPen pen(Qt::DotLine);
    QString s;
    //painter.setPen(pen);
    //painter.drawText(10,10,"hello word");
    painter.drawLine(0,0,0,200);
    painter.drawLine(0,200,200,200);
    //painter.drawLine(0,300,0,500);
    //painter.drawLine(0,500,200,500);
    //painter.setPen(pen);
    for(i=0;i<10;i++)
    {
        painter.drawLine(0,a,200,a);
        a=a+20;
    }
    for(i=0;i<10;i++)
    {
        painter.drawLine(b,0,b,200);
        b=b+20;
    }
    /*for(i=0;i<10;i++)
    {
        painter.drawLine(0,d,200,d);
        d=d+20;
    }
    for(i=0;i<10;i++)
    {
        painter.drawLine(c,300,c,500);
        c=c+20;
    }*/
    pen.setStyle(Qt::SolidLine);
    pen.setColor(QColor(255,0,0,255));
    painter.setPen(pen);



    for(i=0;i<20;i++)
    {
        painter.drawLine(x[i],y[i],x[i+1],y[i+1]);
        //painter.drawLine(x[i],y1[i],x[i+1],y1[i+1]);


    }
    /*pen.setStyle(Qt::SolidLine);
    pen.setColor(QColor(28,30,50,255));
    painter.setPen(pen);
    for(i=0;i<20;i++)
    {
       s='('+QString::number(x[i])+','+QString::number(y[i])+')';
       painter.drawText(x[i],y[i],s);
    }
    for(i=0;i<20;i++)
    {
       s='('+QString::number(x[i])+','+QString::number(y1[i])+')';
       painter.drawText(x[i],y1[i],s);
    }*/

}


void wangge::timerEvent(QTimerEvent *)
{
//    time->setText(QTime::currentTime().toString());
//   int i=0;
//   for(i=0;i<20;i++)
//   {
//       y[i]=y[i+1];
//       //y1[i]=y1[i+1];
//   }
//   y[20]=qrand()%200;
//   while(1)
//   {
//       y1[20]=qrand()%500;
//       if(y1[20]>299) break;
//   }


   this->repaint();

}



void wangge::set_cpu(QString s)
{
    this->time->setText(s);
    int i=0;
    for(i=0;i<20;i++)
    {
        y[i]=y[i+1];
       //y1[i]=y1[i+1];
    }
    QString a=s;
    int per = a.toInt();
    y[20]=200-2*per;
    this->update();
    //QString a="120"
   // int b;
    //b=a.toInt（）


}
